package com.use.service;

import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.entity.Report_electri_Power;
import com.interfaces.*;
import com.utils.JsonResult;

import springfox.documentation.spring.web.json.Json;

/**
 * @Title: ElectricityUseService.java
 * @Package com.use.service
 * @author 孙梦楠
 * @date 2017年10月27日 下午12:44:34
 * @version V1.0.0
 */
@Service
public class ElectricityUseService {

	@Autowired
	private CustomerMeterDao customermeterdao;

	@Autowired
	private CustomerDao customerdao;

	@Autowired
	private MeterDao meterdao;

	@Autowired
	private Report_electri_PowerDao electripowerdao;
	
	public static final Integer Day_size = 30;
	


	// 根绝户号查昨天以及30天之前的电量
	public JsonResult<Object[][]> listUse(String customeraccount, String code) {

		List<Report_electri_Power> uses = electripowerdao
				.findByMeteraddressAndCodeOrderByTimeDesc(findMeteraddressByCustomeraccount(customeraccount), code);


		if (uses.size() != 0 && uses.size() < Day_size) {// 记录不足30天
			Object[][] obj = new Object[uses.size()][4];
			int i = 0;
			for (Report_electri_Power use : uses) {
				obj[i][0] = use.getTime().substring(5,10);
				obj[i][1] = use.getValue1();//总
				DecimalFormat  df  = new DecimalFormat("######0.00");   
				Double value = Double.valueOf(use.getValue3())+Double.valueOf(use.getValue2())+Double.valueOf(use.getValue4());
				obj[i][2] = df.format(value);//锋 +尖+平
				obj[i][3] = use.getValue5();//谷
				i++;
			}
			return new JsonResult<Object[][]>(21, "用量列表", obj);

		} else if (uses.size() != 0 && uses.size() >= Day_size) {
			// 记录超过30天，只显示30天
			Object[][] obj = new Object[Day_size][4];
			int i = 0;
			for (int j = 0; j < Day_size; j++) {
				Report_electri_Power use = uses.get(j);
				obj[i][0] = use.getTime().substring(5,10);
				obj[i][1] = use.getValue1();//总
				obj[i][2] = use.getValue3()+use.getValue2()+use.getValue4();//锋 +尖+平
				obj[i][3] = use.getValue5();//谷
				i++;
			}

			return new JsonResult<Object[][]>(21, "用量列表", obj);
		} else
			return new JsonResult<Object[][]>(22, "暂无数据", null);
	}

	// 通过户号查到对应的表id
	public String findMeteraddressByCustomeraccount(String customeraccount) {
		Integer customerid = customerdao.findByCustomeraccount(customeraccount).getCustomerid();
		Integer meterid = customermeterdao.findByCustomerid2(customerid).getPk().getMeterid();
		return meterid.toString();
	}

}
